package org.apache.tapestry5.csrfprotection.victimapp.components;

import org.apache.tapestry5.*;
import org.apache.tapestry5.annotations.*;
import org.apache.tapestry5.csrfprotection.victimapp.pages.Index;
import org.apache.tapestry5.ioc.annotations.*;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.BindingConstants;

import util.AuthenticationState;

/**
 * Layout component for pages of application victimapp.
 */
@Import(stylesheet="context:layout/layout.css")
public class Layout
{
	@SessionState(create=true)
	@Property
	private AuthenticationState authState;
	
    /** The page title, for the <title> element and the <h1> element. */
    @Property
    @Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
    private String title;
    
    @Parameter("auth")
    private AuthenticationState auth;
    
    public AuthenticationState getAuth(){
    	return authState;
    }

    @Property
    private String pageName;

    @Inject
    private ComponentResources resources;

    public String getClassForPageName()
    {
      return resources.getPageName().equalsIgnoreCase(pageName)
             ? "current_page_item"
             : null;
    }

    public String[] getPageNames()
    {
      return new String[] { "Index" };
    }
    
    @InjectPage
	private Index home;
    
    @Inject
    private Request request; 
    
   
    @Log   
	public Object onActionFromLogout(){
		auth.setLoggedIn(false);
		request.getSession(false).invalidate();
		
		return home;
	}
}
